//
//  Test.swift
//  mj-01
//
//  Created by 宋宏康 on 2021/7/11.
//

import Cocoa

class ArrayList<T: Comparable> {
    // 动态数组的长度
    var size: Int {
        get {
            return elements.count
        }
    }
    
    // 动态数组存储的元素
    var elements: [T] = Array<T>()
    
    // 常量，动态数组的默认容量
    private final let DEFAULT_CAPATICY: Int = 10;
    
    // 有参构造器
    init(capaticy: Int) {
        if capaticy >= 1 {
            self.elements.reserveCapacity(capaticy)
        }
        else {
            self.elements.reserveCapacity(DEFAULT_CAPATICY)
        }
    }
    
    // 无参构造器(默认容量是10)
    init() {
        elements.reserveCapacity(DEFAULT_CAPATICY)
    }
    
    // 是否为空
    func isEmpty() -> Bool {
        return self.elements.isEmpty;
    }
    
    // 是否包含某个元素
    func contains(element: T) -> Bool {
        return self.elements.contains(element)
    }
    
    // 添加元素到最后面
    func add(element: T) -> Void {
        self.elements.append(element)
    }
    
    // 往index位置插入元素
    func add(index: Int, element: T) throws -> Void {
        if index < 0 || index >= self.elements.capacity {
            throw VendingMathineError.indexOutOfBounds(index: index, size: self.size)
        }
        
        let length = self.elements.count - 1
        for i in length..<self.elements.count {
            elements[i+1] = elements[i]
        }
        elements[index] = element
    }
    
    // 返回index位置对应的元素
    func get(index: Int) throws -> T {
        if index < 0 || index >= self.size {
            throw VendingMathineError.indexOutOfBounds(index: index, size: self.size)
        }
        return self.elements[index]
    }
    
    /// 往index位置设置元素
    ///
    /// - Parameters:
    ///   - index: index
    ///   - element: element
    /// - Returns: 原来的元素
    func set(index: Int, element: T) throws -> T {
        if index < 0 || index >= self.size {
            throw VendingMathineError.indexOutOfBounds(index: index, size: self.size)
        }
        
        let old = self.elements[index]
        self.elements[index] = element;
        return old
    }
    
    // 删除index位置对应的元素
    func remove(index: Int) -> T {
        return self.elements.remove(at: index)
    }
    
    // 查看元素的位置
    func indexOf(element: T) -> Int {
        for i in 0..<self.elements.count {
            if self.elements[i] == element {
                return i
            }
        }
        return -1
    }
    
    // 清除所有元素
    func clear() {
        self.elements.removeAll()
    }
    
    func toString() {
        for i in 0..<self.elements.count {
            print("数组中存放元素有：\(self.elements[i])")
        }
    }
    
    enum VendingMathineError: Error {
        case invalidSelection
        case insufficientFunds(coinsNeed: Int)
        case indexOutOfBounds(index: Int, size: Int)
    }
}

var arr = ArrayList<Int>(capaticy: 5);
//let item = try? arr.get(index: 5)
print(arr.isEmpty())
print(arr.size)

arr.add(element: 10)
try? arr.add(index: 0, element: 20)
print(arr.toString())
